// pages/counselor/xuesheng/xuesheng.js
Page({
  data: {
    searchText: '',
    classList: [
      { id: 0, name: '全部班级' },
      { id: 1, name: '2023级计算机科学1班' },
      { id: 2, name: '2023级软件工程1班' },
      { id: 3, name: '2022级数据科学1班' }
    ],
    classIndex: 0,
    statusList: ['全部', '正常', '请假', '警告'],
    statusIndex: 0,
    studentList: [],
    isLoading: false
  },

  onLoad: function() {
    this.fetchStudentList();
  },
  
  onShow: function() {
    // 页面显示时刷新数据
    this.fetchStudentList();
  },
  
  // 搜索输入
  onSearchInput: function(e) {
    this.setData({
      searchText: e.detail.value
    });
    
    // 延迟搜索，避免频繁请求
    if (this.searchTimer) {
      clearTimeout(this.searchTimer);
    }
    
    this.searchTimer = setTimeout(() => {
      this.fetchStudentList();
    }, 500);
  },
  
  // 班级选择
  classChange: function(e) {
    this.setData({
      classIndex: e.detail.value
    });
    
    this.fetchStudentList();
  },
  
  // 状态选择
  statusChange: function(e) {
    this.setData({
      statusIndex: e.detail.value
    });
    
    this.fetchStudentList();
  },
  
  // 获取学生列表
  fetchStudentList: function() {
    this.setData({ isLoading: true });
    
    const classId = this.data.classList[this.data.classIndex].id;
    const status = this.data.statusList[this.data.statusIndex];
    
    wx.request({
      url: 'http://localhost:3000/api/counselor/students',
      method: 'GET',
      data: {
        keyword: this.data.searchText,
        classId: classId === 0 ? '' : classId,
        status: status === '全部' ? '' : status
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            studentList: res.data
          });
        }
      },
      fail: () => {
        // 使用模拟数据
        const mockStudents = [
          { 
            id: 1, 
            name: '张三', 
            studentId: '2023001', 
            className: '2023级计算机科学1班',
            phone: '13800138001', 
            email: 'zhangsan@example.com', 
            status: '正常', 
            avatar: '/images/avatar/student1.png' 
          },
          { 
            id: 2, 
            name: '李四', 
            studentId: '2023002', 
            className: '2023级计算机科学1班', 
            phone: '13800138002', 
            email: 'lisi@example.com', 
            status: '请假', 
            avatar: '/images/avatar/student2.png' 
          },
          { 
            id: 3, 
            name: '王五', 
            studentId: '2023003', 
            className: '2023级软件工程1班', 
            phone: '13800138003', 
            email: 'wangwu@example.com', 
            status: '正常', 
            avatar: '/images/avatar/student3.png' 
          },
          { 
            id: 4, 
            name: '赵六', 
            studentId: '2023004', 
            className: '2023级软件工程1班', 
            phone: '13800138004', 
            email: 'zhaoliu@example.com', 
            status: '警告', 
            avatar: '/images/avatar/student4.png' 
          },
          { 
            id: 5, 
            name: '钱七', 
            studentId: '2022001', 
            className: '2022级数据科学1班', 
            phone: '13800138005', 
            email: 'qianqi@example.com', 
            status: '正常', 
            avatar: '/images/avatar/student5.png' 
          }
        ];
        
        // 根据筛选条件过滤
        let filteredStudents = [...mockStudents];
        
        if (this.data.classIndex !== 0) {
          const className = this.data.classList[this.data.classIndex].name;
          filteredStudents = filteredStudents.filter(student => student.className === className);
        }
        
        if (this.data.statusIndex !== 0) {
          const status = this.data.statusList[this.data.statusIndex];
          filteredStudents = filteredStudents.filter(student => student.status === status);
        }
        
        if (this.data.searchText) {
          const searchText = this.data.searchText.toLowerCase();
          filteredStudents = filteredStudents.filter(student => 
            student.name.toLowerCase().includes(searchText) || 
            student.studentId.toLowerCase().includes(searchText)
          );
        }
        
        this.setData({
          studentList: filteredStudents,
          hasMore: false
        });
      },
      complete: () => {
        this.setData({ isLoading: false });
        wx.stopPullDownRefresh();
      }
    });
  },
  
  // 查看学生详情
  viewStudentDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/counselor/xuesheng/detail/detail?id=${id}`
    });
  },
  
  // 发送通知
  sendMessage: function() {
    wx.navigateTo({
      url: '/pages/counselor/xiaoxi/send/send'
    });
  },
  
  // 导出学生名单
  exportStudentList: function() {
    wx.showLoading({
      title: '导出中...',
    });
    
    const classId = this.data.classList[this.data.classIndex].id;
    const status = this.data.statusList[this.data.statusIndex];
    
    wx.request({
      url: 'http://localhost:3000/api/counselor/students/export',
      method: 'POST',
      data: {
        classId: classId === 0 ? '' : classId,
        status: status === '全部' ? '' : status,
        search: this.data.searchText
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        wx.hideLoading();
        
        if (res.statusCode === 200) {
          wx.showModal({
            title: '导出成功',
            content: '学生名单已导出，请到系统后台下载',
            showCancel: false
          });
        } else {
          wx.showToast({
            title: res.data.message || '导出失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.hideLoading();
        
        // 模拟成功
        wx.showModal({
          title: '导出成功',
          content: '学生名单已导出，请到系统后台下载',
          showCancel: false
        });
      }
    });
  },
  
  // 考勤统计
  checkAttendance: function() {
    wx.navigateTo({
      url: '/pages/counselor/kaoqin/kaoqin'
    });
  },
  
  // 下拉刷新
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      studentList: []
    });
    this.fetchStudentList();
  },
  
  // 上拉加载更多
  onReachBottom: function() {
    if (this.data.hasMore) {
      this.fetchStudentList();
    }
  }
})